* Cleans and organizes choice moments data exported from fe_preference_estimation.m
* Generates histograms of choices and reversals

clear
set more off
pause on

cd ""

insheet using choice_moments.csv, clear
* Variable explanations:
*time_index-number of distant choices made - sum on all time choice tasks - out of 48
*risk_index-number of risky choices made - sum on all risk choice tasks - out of 55
*_reversal_n - number of reversals (switching back to safe after a risky choice or to near after a distant choice within a given risk/time choice set. Separated, by risk - OLS, MPL, time, total
* risk/time_1-8: For each risk/time choice set, the 1st question at which an individual picks the riskier/more distant option. Afterwards, only risky/distant option selections would be expected for the remainder of the choice set; otherwise a reversal.
rename(v1-v22) (ind_id time_index lottery_index time_reversal_n risk_reversal_n reversal_n risk1 risk2 risk3 risk4 risk5 risk6 risk7 risk8 time1 time2 time3 time4 time5 time6 time7 time8  )

* if no switch occurs, recode as if switch was on the N+1th task
foreach var of varlist risk1-risk3 {
	replace `var'="11" if `var'=="NaN"
	destring `var', replace
	}
foreach var of varlist risk4-risk8 {
	replace `var'="6" if `var'=="NaN"
	destring `var', replace
	}
foreach var of varlist time1-time4 {
	replace `var'="7" if `var'=="NaN"
	destring `var', replace
	}
foreach var of varlist time5-time8 {
	replace `var'="7" if `var'=="NaN"
	destring `var', replace
	}	

* Recodes the index variables such that high values correspond to high risk or delay aversion (analogous to the risk aversion and discount rate parameters in full model so that regression signs are comparable).
gen inv_time_index=abs(time_index-48)
gen inv_lottery_index=abs(lottery_index-55)
*gets the standard deviation of the switching point in each group set, where one would expect the same switching point in each group in a deterministic framework under CRRA utility
egen temp_switch_sd=rowsd(time1-time8)
egen risk_MPL_sd=rowsd(risk1-risk3)


sort ind_id
save data_choice_moments, replace


* Generates Figures 1 and 2 of paper
* Generates histograms of choices and reversals

graph drop _all
local scale=300
local var reversal_n
capture sum `var', detail
local med=`r(p50)'
local mean=`r(mean)'
local var risk_reversal_n2
capture sum fe_`var', detail	 
local med_fe=`r(p50)'
local lab="# of reversals in choices"


* Figure 2
twoway hist reversal_n, fcolor(green) lcolor(black) freq xlabel(0(5)15) xmtick(0(1)15) discrete start(0) width(1) || scatteri 0 `mean' `scale' `mean', lcolor(red) lpattern(line) c(l) m(i) lwidth(medthick)  || scatteri 0 `med' `scale' `med', lcolor(blue) lpattern(dash) c(l) m(i) lwidth(medthick) legend(label(1 "Reversals") label(2 "Mean") label(3 "Median")) xtitle("`lab'") ytitle("frequency")
graph export g_reversal.pdf,replace


local i=1
foreach var of varlist inv_time_index  inv_lottery_index {
	 capture sum `var', detail
	 local mean=`r(mean)'
	 local med=`r(p50)'


	
	if "`var'"=="inv_time_index" {
		local lab="# of impatient choices"
		local scale=100
		local int=6
		local max=48
		local m_int=1
		local m_max=48			
		}

		
	if "`var'"=="inv_lottery_index" {
		local lab="# of safe choices"
		local scale=75
		local int=5
		local max=55	
		local m_int=1
		local m_max=55			
		}

	local yw=`scale'/4	
	*** added bins as a new thing, check that all is OK
	twoway hist `var', freq bin(`m_max') xlabel(0(`int')`max') xmtick(0(`m_int')`m_max') ylabel(0(`yw')`scale') || scatteri 0 `mean' `scale' `mean', lcolor(red) lpattern(dash) c(l) m(i) lwidth(medthick) || scatteri 0 `med' `scale' `med', lcolor(blue) lpattern(dash) c(l) m(i) lwidth(medthick) name(tm`i') legend(label(1 "Frequency") label(2 "Mean") label(3 "Median")) xtitle("`lab'") ytitle("frequency")
	local ++i
	}

* need to install grc1leg  
* Figure 1	
grc1leg tm2 tm1  
graph export g_choices.pdf,replace














